package entity;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.GregorianCalendar;


/**
 * @author Khoa Nguyen
 * 
 */
public class Submission {

	static Calendar cal = new GregorianCalendar();
	
	private String confirmNo;

	private String studentId;

	private String subjectId;

	private String assNo;

	private String fileName;

	private String sDate;

	private String Status;

	private String studentData = "";

	private String date;

	private String confirmationNo;

	private long submittedTimeInMilli;

	/**
	 * 
	 */
	
	public Submission(String sid) {
		this.studentId = sid;
	}
	
	public void addData(String subject, String file,
			String confirmNo, long time) {
		
	    // Get the components of the date
	    int year = cal.get(Calendar.YEAR);             // 2002
	    int month = cal.get(Calendar.MONTH);           // 0=Jan, 1=Feb, ...
	    int day = cal.get(Calendar.DAY_OF_MONTH);      // 1...
	    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 1=Sunday, 2=Monday, ...
	    
		// TODO Auto-generated constructor stub
		this.fileName = file;
		this.subjectId = subject;
		this.submittedTimeInMilli = time;
		this.confirmationNo = confirmNo;

		this.studentData += this.getStudentId() + " | " + this.getSubjectId()
				+ " | " + this.getFileName() + " | " + this.getConfirmationNo()
				+ " | " + this.getSubmittedTimeInMilli() + " * ";
		
		// save to DB (file )
		
		FileOutputStream dataOut;	
		try
		{
		    // Open an output stream
		    dataOut = new FileOutputStream("Data.txt", true);

		    // Print a line of text
		    new PrintStream(dataOut).println ("======================================================================");
		    new PrintStream(dataOut).println ("Confirmation number : " + this.getStudentId() + "_" + this.getSubjectId() + "_" + this.getSubmittedTimeInMilli());
		    new PrintStream(dataOut).println ("Student: " + this.getStudentId());
		    new PrintStream(dataOut).println ("Subject: " + this.getSubjectId());
		    new PrintStream(dataOut).println ("Submited file: " + this.getFileName());
		    new PrintStream(dataOut).println ("Submited date: " + day + "/" + month + "/" + year + "Time: " + cal.getTime());
		    new PrintStream(dataOut).println ("Status: pending");
		    new PrintStream(dataOut).println ("Mark: unmarked");
		    new PrintStream(dataOut).println ("");
		    
		    
		    // Close our output stream
		    dataOut.close();		
		}
		// Catches any error conditions
		catch (IOException e)
		{
			System.err.println ("Unable to write to file");
			System.exit(-1);
		}
		
		// Out put confirmation to file
		// Stream to write file
		FileOutputStream fout;	
		try
		{
		    // Open an output stream
		    fout = new FileOutputStream ("SubmitOutput.txt");

		    // Print a line of text
		    new PrintStream(fout).println ("======================================================================");
		    new PrintStream(fout).println ("Confirmation number : " + this.getStudentId() + "_" + this.getSubjectId() + "_" + this.getSubmittedTimeInMilli());
		    new PrintStream(fout).println ("Student: " + this.getStudentId());
		    new PrintStream(fout).println ("Subject: " + this.getSubjectId());
		    new PrintStream(fout).println ("Submited file: " + this.getFileName());
		    new PrintStream(fout).println ("Submited date: " + day + "/" + month + "/" + year + "Time: " + cal.getTime());
		    new PrintStream(fout).println ("======================================================================");
		    
		    
		    // Close our output stream
		    fout.close();		
		}
		// Catches any error conditions
		catch (IOException e)
		{
			System.err.println ("Unable to write to file");
			System.exit(-1);
		}
		
		
		
	}

	public void readFile()
	{
		boolean has = false; 
		FileInputStream fin;
		DataInputStream din;		
		FileOutputStream fout;
		
		String str;
		try
		{
			
			fin = new FileInputStream("Data.txt");
			din = new DataInputStream(fin);
			fout = new FileOutputStream("ViewStatusOutput.txt");
			
			
			while ((str = din.readLine()) != null) {
				
				if (str.contains(this.getStudentId()))
				{
					has = true;
					new PrintStream(fout).println(str); 
					for (int i=0;i < 7; i++)
					{
						str = din.readLine();
						new PrintStream(fout).println(str);
					}
					new PrintStream(fout).println("=================================");
					new PrintStream(fout).println();
					
				}
				
			}

		    // Close our input stream
		    fin.close();		
		
		} catch (Exception e)
		{
			System.exit(-1);
		}
	}
	
	
	public String returnData(String id) {
		
		readFile();
		/*
		String allInfo = this.getStudentData();
		String forDisplay = "";
		int loop = 0;
		
		String StudentInfo[] = allInfo.split(" * ");
		for (int i = 0; i < StudentInfo.length; i++) {
			String StudentDataInfo[] = StudentInfo[i].split(" | ");
			if (id == StudentDataInfo[0]) {
				if (loop == 0 ) {
				forDisplay += "Student ID: " + StudentDataInfo[0];
				forDisplay += "\n---------------------------------------------------\n";
				}
				forDisplay += "Subject Code: " + StudentDataInfo[1] + "\n";
				forDisplay += "Assignment Submitted for this Subject Code: "
						+ StudentDataInfo[2] + "\n";
				forDisplay += "Submission Date: " + StudentDataInfo[4] + "\n";
				forDisplay += "Confirmation (Receipt) Number: "
						+ StudentDataInfo[3] + "\n";
				forDisplay += "---------------------------------------------------\n";
				
				loop++;
			}
		}
		*/
		return "Cool! - Check out put file";
		
	}

	public void setConfirmNo() {
		// TODO
	}

	public String getConfirmNo() {
		// TODO
		return confirmNo;
	}

	/** ********************************** */

	public String getAssNo() {
		return assNo;
	}

	public void setAssNo(String assNo) {
		this.assNo = assNo;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSDate() {
		return sDate;
	}

	public void setSDate(String date) {
		sDate = date;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public void setConfirmNo(String confirmNo) {
		this.confirmNo = confirmNo;
	}

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

	public String getSubjectId() {
		return subjectId;
	}

	public void setSubjectId(String subjectId) {
		this.subjectId = subjectId;
	}

	public String getStudentData() {
		return studentData;
	}

	public void setStudentData(String studentData) {
		this.studentData = studentData;
	}

	public long getSubmittedTimeInMilli() {
		return submittedTimeInMilli;
	}

	public void setSubmittedTimeInMilli(long submittedTimeInMilli) {
		this.submittedTimeInMilli = submittedTimeInMilli;
	}

	public String getConfirmationNo() {
		return confirmationNo;
	}

	public void setConfirmationNo(String confirmationNo) {
		this.confirmationNo = confirmationNo;
	}

}
